package com.uenpay.im.ui.old.activity.bankcard;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @version 1.0
 * @E-mail:
 * @date 创建时间：2016-7-11 下午12:18:59
 * @parameter
 * @Function 银行卡列表
 */
public class BankListActivity extends BaseActivity {

    private BankCardAdapter mBankCardAdapter;
    private ListView listview;
    private String selectedBankCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_list_layout;
    }

    @Override
    public void initView(View view) {
        selectedBankCard = getIntent().getStringExtra("selectedBankCard");
        initView();
        initListener();
        getBankCardList();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.bank_crad);
        listview = (ListView) findViewById(R.id.listview);
    }

    protected void initListener() {
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                String bankCard = (String) arg0.getAdapter().getItem(arg2);
                if (!TextUtils.isEmpty(bankCard)) {
                    mBankCardAdapter.setSelectedBankCard(bankCard);
                    Intent intent = new Intent();
                    intent.putExtra("bankCard", bankCard);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    private void getBankCardList() {
        BankCardHandle.getBankCardList(new BankCardHandle.IGetBankCardListListener() {

            @Override
            public void onBankCardList(int status, String msg,
                                       ArrayList<String> cards) {
                if (status == HttpInvokeResultCode.RESULT_OK && cards != null) {
                    mBankCardAdapter = new BankCardAdapter(
                            BankListActivity.this, selectedBankCard, cards);
                    listview.setAdapter(mBankCardAdapter);
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });
    }

}
